import { isSeller, isBuyer, isLine } from "@/util/common";
import PriceType from "@/components-business/quotation/priceType";
import OrderStatus from "@/components-business/order/status";
import LineOrderStatus from "@/components-business/order-line/status";
import DealStatus from "@/components-business/order/dealStatus";
import HandleBtn from "@/components-business/order/handleBtn";
import HandleBtnLine from "@/components-business/order-line/handleBtn-line";
import quotationDict from "@/dict/quotation";
import orderDict from "@/dict/order";
import instance from "@/main";
import OrderTag from "./components/tag.vue";

let h = instance.$createElement;

export const columns = {
  data() {
    // 用于列合并
    const renderContent = (text, record) => {
      const obj = {
        children: text,
        attrs: {}
      };
      if (record.hedgeRow) {
        obj.attrs.colSpan = 0;
      }
      return obj;
    };
    // 插槽渲染列
    const slotRenderContent = (text, record, index, render) => {
      const obj = {
        children: text,
        attrs: {}
      };
      if (record.hedgeRow) {
        obj.attrs.colSpan = 0;
        return obj;
      } else {
        obj.children = render(record);
        return obj;
      }
    };
    // 标签
    const tagRender = (row, idx) => {
      return (
        <OrderTag
          orderData={{ ...row, index: idx }}
          tags={this.tags}
          onSelectTag={this.selectedTag}
          onUpdateTags={this.updateTags}
        />
      );
    };
    // 点价方式
    const createDateRender = row => {
      return <span>{this.$getTime(row.createDate)}</span>;
    };
    // 点价方式
    const priceTypeRender = row => {
      return <PriceType flag={row} />;
    };
    // 基价
    const basePriceRender = row => {
      return (
        <div class="basePrice">
          <span
            class={row.isBasePriceChanged ? "danger" : ""}
            style={{ marginRight: row.isBasePriceChanged ? "2px" : "0px" }}
          >
            {row.basePrice}
          </span>
          <span class="danger">{row.isBasePriceChanged ? "(修改后)" : ""}</span>
        </div>
      );
    };
    // 含税单价
    const taxRender = row => {
      return (
        //  点价含税单价，基价 + 升贴水
        //  确定价含税单价直接使用firmPrice
        <span>
          {row.priceType === quotationDict.priceType.pricing.value
            ? Number(row.basePrice) + Number(row.premium)
            : row.firmPrice}
        </span>
      );
    };
    // 仓库简称
    const warehouseNameRender = row => {
      return <span>{row.warehouse && row.warehouse.shortName}</span>;
    };
    // 点价人
    const createdUserRender = row => {
      return <span>{row.customerUser && row.customerUser.realName}</span>;
    };
    // 公司简称
    const companyNameRender = row => {
      return (
        <span>
          {isBuyer()
            ? row.providerCompany && row.providerCompany.companyNickname
            : row.customerCompany && row.customerCompany.companyNickname}
        </span>
      );
    };
    // 成交状态
    const orderDealTypeRender = row => {
      return <DealStatus flag={row.orderDealType} />;
    };
    // 订单状态渲染
    const orderStatusRender = row => {
      let res;
      if (isLine()) {
        res = <LineOrderStatus data={row} split={true} />;
      } else {
        res = (
          <div>
            <OrderStatus data={row} split={true} />
            {isSeller() &&
            row.orderStatus ===
              orderDict.pricingStatus.applyModifyPrice.value ? (
              <div class="danger fs-12" style="margin-top:4px;">
                申请改价为{row.alterTargetPrice}
              </div>
            ) : null}
          </div>
        );
      }
      return <div>{res}</div>;
    };
    // 操作列渲染
    const handleRender = row => {
      let res;
      if (isLine()) {
        res = <HandleBtnLine data={row} onHandleSuccess={this.handleSuccess} />;
      } else {
        res = <HandleBtn data={row} onHandleSuccess={this.handleSuccess} />;
      }
      return <div class="handle">{res}</div>;
    };
    return {
      orderDict,
      quotationDict,
      pricingColumns: [
        {
          title: "标签",
          width: 80,
          dataIndex: "tag",
          hidden: isSeller() && !isLine() ? false : true,
          customRender: (text, record, index) => {
            // const obj = {
            //   children: tagRender(record, index),
            //   attrs: {}
            // };
            // return obj;
            let el = h("div", {}, [
              h("i", "⚠️ "),
              h(
                "span",
                {
                  style: {
                    fontSize: "13px"
                  }
                },
                record.hedgeWarningMsg
              )
            ]);
            const obj = {
              children: tagRender(record, index),
              attrs: {}
            };
            if (record.hedgeRow) {
              obj.children = el;
              obj.attrs.colSpan = 20;
              return obj;
            } else {
              return obj;
            }
          }
        },
        {
          title: "订单编号",
          dataIndex: "orderCode",
          width: 150,
          hidden: false,
          customRender: renderContent
          // customRender: (text, record) => {
          //   let el = h("div", {}, [
          //     h("i", "⚠️ "),
          //     h(
          //       "span",
          //       {
          //         style: {
          //           fontSize: "13px"
          //         }
          //       },
          //       record.hedgeWarningMsg
          //     )
          //   ]);
          //   const obj = {
          //     children: text,
          //     attrs: {}
          //   };
          //   if (record.hedgeRow) {
          //     obj.children = el;
          //     obj.attrs.colSpan = 20;
          //     return obj;
          //   } else {
          //     return obj;
          //   }
          // }
        },
        {
          title: "订单状态",
          dataIndex: "orderStatus",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, orderStatusRender);
          }
        },
        {
          title: "成交状态",
          dataIndex: "orderDealType",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, orderDealTypeRender);
          }
        },
        {
          title: "公司简称",
          dataIndex: "companyName",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, companyNameRender);
          }
        },
        {
          title: "点价人",
          dataIndex: "createdUser",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, createdUserRender);
          }
        },
        {
          title: "合约",
          dataIndex: "futuresContract",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "基价",
          dataIndex: "basePrice",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, basePriceRender);
          }
        },
        {
          title: "成交",
          dataIndex: "dealAmount",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "下单",
          dataIndex: "amount",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "升贴水",
          dataIndex: "premium",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "含税单价",
          dataIndex: "tax",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, taxRender);
          }
        },
        {
          title: "含税总金额",
          dataIndex: "totalPrice",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "成交时间",
          dataIndex: "dealDate",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "商品品名",
          dataIndex: "categoryName",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "品牌",
          dataIndex: "brand",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "规格",
          dataIndex: "spec",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "材质",
          dataIndex: "material",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "其他",
          dataIndex: "other",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "关联状态",
          dataIndex: "relevanceStatus",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "可关联数",
          dataIndex: "ableRelevance",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "已关联数",
          dataIndex: "relevanceAmount",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "作价方式",
          dataIndex: "priceType",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, priceTypeRender);
          }
        },
        {
          title: "仓库简称",
          dataIndex: "warehouse",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, warehouseNameRender);
          }
        },
        {
          title: "下单时间",
          dataIndex: "createDate",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, createDateRender);
          }
        },
        {
          title: "操作",
          width: 100,
          dataIndex: "handle",
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, handleRender);
          }
        }
      ],
      confirmColumns: [
        {
          title: "标签",
          width: 80,
          dataIndex: "tag",
          hidden: isSeller() && !isLine() ? false : true,
          customRender: (text, record, index) => {
            // const obj = {
            //   children: tagRender(record, index),
            //   attrs: {}
            // };
            // return obj;
            let el = h("div", {}, [
              h("i", "⚠️ "),
              h(
                "span",
                {
                  style: {
                    fontSize: "13px"
                  }
                },
                record.hedgeWarningMsg
              )
            ]);
            const obj = {
              children: tagRender(record, index),
              attrs: {}
            };
            if (record.hedgeRow) {
              obj.children = el;
              obj.attrs.colSpan = 20;
              return obj;
            } else {
              return obj;
            }
          }
        },
        {
          title: "订单编号",
          dataIndex: "orderCode",
          width: 150,
          hidden: false,
          customRender: renderContent
          // customRender: (text, record) => {
          //   let el = h("div", {}, [h("span", record.hedgeWarningMsg)]);
          //   const obj = {
          //     children: text,
          //     attrs: {}
          //   };
          //   if (record.hedgeRow) {
          //     obj.children = el;
          //     obj.attrs.colSpan = 20;
          //     return obj;
          //   } else {
          //     return obj;
          //   }
          // }
        },
        {
          title: "订单状态",
          dataIndex: "orderStatus",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, orderStatusRender);
          }
        },
        {
          title: "公司简称",
          dataIndex: "companyName",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, companyNameRender);
          }
        },
        {
          title: "点价人",
          dataIndex: "createdUser",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, createdUserRender);
          }
        },
        {
          title: "下单",
          dataIndex: "amount",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "含税单价",
          dataIndex: "tax",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, taxRender);
          }
        },
        {
          title: "含税总金额",
          dataIndex: "totalPrice",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "成交时间",
          dataIndex: "dealDate",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "商品品名",
          dataIndex: "categoryName",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "品牌",
          dataIndex: "brand",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "规格",
          dataIndex: "spec",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "材质",
          dataIndex: "material",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "其他",
          dataIndex: "other",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "关联状态",
          dataIndex: "relevanceStatus",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "可关联数",
          dataIndex: "ableRelevance",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "已关联数",
          dataIndex: "relevanceAmount",
          hidden: false,
          customRender: renderContent
        },
        {
          title: "作价方式",
          dataIndex: "priceType",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, priceTypeRender);
          }
        },
        {
          title: "仓库简称",
          dataIndex: "warehouse",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, warehouseNameRender);
          }
        },
        {
          title: "下单时间",
          dataIndex: "createDate",
          hidden: false,
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, createDateRender);
          }
        },
        {
          title: "操作",
          width: 100,
          dataIndex: "handle",
          customRender: (text, record, index) => {
            return slotRenderContent(text, record, index, handleRender);
          }
        }
      ]
    };
  }
};
